package com.ziyueyuwen.xiniao.controller.classtest;

import com.alibaba.fastjson.JSONObject;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.*;
import com.ziyueyuwen.xiniao.bean.homework.ZySubjectQuestionDO;
import com.ziyueyuwen.xiniao.bean.vo.ZyClassVO;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.service.*;
import com.ziyueyuwen.xiniao.util.ConstUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.util.ConstUtil.PARAM_EEO_CLASS_ID;
import static com.ziyueyuwen.xiniao.util.ConstUtil.PARAM_EEO_U_ID;

/**
 * Created by wancheng on 2019/4/19.
 */
@Controller
@RequestMapping("/class/test")
public class ClassTestController {

    @Autowired
    LessonService lessonService;
    @Autowired
    UserService userService;
    @Autowired
    SubjectQuestionService subjectQuestionService;
    @Autowired
    LessonUserService lessonUserService;
    @Autowired
    CourseService courseService;
    @Autowired
    ClassService classService;

    @RequestMapping(value = "/user_lesson_msg.do", method = RequestMethod.POST)
    public void UserAndLessonMsg(@RequestParam(PARAM_EEO_CLASS_ID) Integer eeoClassId,
                                 @RequestParam(PARAM_EEO_U_ID)String uId) throws Exception {
        HttpResContent<Map<String,Object>> hrc = new HttpResContent<>("获取用户在课堂互动中的基础信息");
        checkParam(ParamTypeEnum.INTEGER_GT_0,PARAM_EEO_CLASS_ID,eeoClassId);
        checkParam(ParamTypeEnum.STRING_NOT_BLANK,PARAM_EEO_U_ID,uId);
        ZyClassLessonInfo lesson = lessonService.getLessonInfoByEeoClassId(eeoClassId);
        if (lesson == null){
            throw new Exception("无此正式课信息");
        }
        ZyUser user = userService.getUserByUid(uId);
        if (user == null){
            throw new Exception("无此用户信息");
        }
        ZyClassLessonUser param = new ZyClassLessonUser();
        param.setUserAccount(user.getUserAccount());
        param.setClassLessonId(lesson.getId());
        ZyClassLessonUser lessonUser = lessonUserService.getLessonUser(param);
        JSONObject test = subjectQuestionService.getClassroomTestQuestion(eeoClassId);
        ZyClassVO classInfo = classService.getClassInfoByClassId(lesson.getClassId());
        ZySubjectQuestionDO subjectQuestionDO = subjectQuestionService.getBySubjectIdAndHomeworkIndex(classInfo.getCourse().getSubjectId(),lesson.getLessonIndex());
        Map<String,Object> res = new HashMap<>(4);
        res.put("lesson",lesson);
        res.put("user",user);
        if (lessonUser != null){
            res.put("role",lessonUser.getType());
        }else{
            res.put("role","STUDENT");
        }
        res.put("test",test);
        res.put("subjectQuestionId",subjectQuestionDO.getId());
        hrc.printLogAndResponse(res);
    }
}
